package com.atguigu.datastream.test.day03;

import com.atguigu.datastream.bean.Event;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink_06_Transfomat_map
 * Package: com.atguigu.test.day03
 * Description:
 *        filter算子
 * @Author ChenJun
 * @Create 2023/4/9 20:05
 * @Version 1.0
 */
public class Flink_07_Transfomat_filter {
    public static void main(String[] args) throws Exception {

        //1. 创建Flink 流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //2. 设置并行都
        env.setParallelism(1);

        //3 . 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 将读进来的字符串转为Event
        SingleOutputStreamOperator<Event> map = streamSource.map(new MapFunction<String, Event>() {
            @Override
            public Event map(String s) throws Exception {
                System.out.println("-------map--------");
                String[] split = s.split(",");

                return new Event(split[0], split[1], Long.parseLong(split[2]));
            }
        });


        SingleOutputStreamOperator<Event> filter = map.filter(new FilterFunction<Event>() {
            @Override
            public boolean filter(Event event) throws Exception {
                System.out.println("--------filter------------");
                return "songsong".equals(event.user);
            }
        });

        //4. 打印
        filter.print();

        //5. 执行
        env.execute();

    }
}
